#include <stdio.h>
#include <string.h>
#define STR_LEN 10

void tolerance(char str[]);
int check(char str[]);
int search(char str[]);

int main()
{
    char str[STR_LEN+1];
    tolerance(str);
    if(search(str)==1)
    {
        printf("You are welcome!\n");
    }
    else
    {
        printf("You are Illegal User!\n");
    }
    return 0;
}

void tolerance(char str[])
{
    do
    {
        printf("Please input a password:");
        fgets(str,STR_LEN+1,stdin);
    } while (check(str));
    return;
}

int check(char str[])
{
    int i;
    int len=strlen(str);
    for(i=0;i<len-1;i++)
    {
        if(str[i]<str[i+1])//检验结果是升序
        {
            return 1;
        }
    }
    return 0;
}

int search(char str[])
{
    char ch;
    int len = strlen(str);
    int min=len-1,max=0;
    int mid;
    printf("Please input a character:");
    scanf(" %c",&ch);
    while (min >= max)
    {
        mid = (min + max) / 2;
        if (ch == str[mid])
        {
            return 1;
        }
        else if(ch==str[min])
        {
            return 1;
        }
        else if(ch==str[max])
        {
            return 1;
        }
        else if (ch < str[mid])
        {
            max = mid + 1;
        }
        else
        {
            min = mid - 1;
        }
    }
    return 0;
}